package com.ruoyi.customerSample.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;

import com.ruoyi.customerSample.domain.SampleSendDetails;
import com.ruoyi.customerSample.domain.SampleSendInfo;
import com.ruoyi.customerSample.service.ISampleSendDetailsService;
import com.ruoyi.customerSample.service.ISampleSendInfoService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 来样登记信息Controller
 * 
 * @author ruoyi
 * @date 2023-07-05
 */
@RestController
@RequestMapping("/sample/send")
@Api(value="来样登记信息",tags = "来样登记信息接口")
public class SampleSendInfoController extends BaseController
{
    @Autowired
    private ISampleSendInfoService sampleSendInfoService;

    @Autowired
    private ISampleSendDetailsService sampleSendDetailsService;


    /**
     * 查询来样登记信息列表
     */
    @GetMapping("/list")
    @ApiOperation(value="查询来样登记信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "pageNum", value = "页数", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "每页记录数", required = true, dataType = "String"),
    })
    public TableDataInfo list(SampleSendInfo sampleSendInfo)
    {
        startPage();
        List<SampleSendInfo> list = sampleSendInfoService.selectSampleSendInfoList(sampleSendInfo);
        return getDataTable(list);
    }


    /**
     * 导入模板下载
     * @return
     */
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<SampleSendDetails> util = new ExcelUtil<SampleSendDetails>(SampleSendDetails.class);
        return util.importTemplateExcel("来样登记信息");
    }

    /**
     * 导出来样登记信息列表
     */
    @Log(title = "来样登记信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SampleSendInfo sampleSendInfo)
    {
        List<SampleSendInfo> list = sampleSendInfoService.selectSampleSendInfoList(sampleSendInfo);
        ExcelUtil<SampleSendInfo> util = new ExcelUtil<SampleSendInfo>(SampleSendInfo.class);
        return util.exportExcel(list, "info");
    }

    /**
     * 根据仪器类别获取最大编号
     */
    @Log(title = "根据仪器类别获取最大编号", businessType = BusinessType.EXPORT)
    @GetMapping("/getMaxNum/{sampleNum}")
    @ApiOperation(value="根据仪器类别获取最大编号")
    @ApiImplicitParam(paramType="query", name = "sampleNum", value = "仪器类别", required = true, dataType = "String")
    public AjaxResult getMaxNum(@PathVariable("sampleNum") String sampleNum)
    {
        return AjaxResult.success(sampleSendDetailsService.selectNumMax(sampleNum));
    }

    /**
     * 报告信息导入
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<SampleSendDetails> util = new ExcelUtil<SampleSendDetails>(SampleSendDetails.class);
        List<SampleSendDetails> equInfoList = util.importExcel(file.getInputStream());
        //对象元素转换
        Function<SampleSendDetails, List<Object>> compositeKey = sampleSendDetails ->
                Arrays.asList(sampleSendDetails.getReDate(), sampleSendDetails.getClient(), sampleSendDetails.getClientAdress(), sampleSendDetails.getSampleReceiver(), sampleSendDetails.getSampleSender(), sampleSendDetails.getPhonenum());
        // 分组
        Map<List<Object>, List<SampleSendDetails>> groupingMap =
                equInfoList.stream().collect(Collectors.groupingBy(compositeKey, Collectors.toList()));
        //数据导入
        sampleSendInfoService.importSampleSendInfo(groupingMap);
        return AjaxResult.success();
    }


    /**
     * 获取来样登记信息详细信息
     */
    @GetMapping(value = "/{id}")
    @ApiOperation(value="获取来样登记信息详细信息")
    @ApiImplicitParam(paramType="query", name = "id", value = "主键", required = true, dataType = "String")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(sampleSendInfoService.selectSampleSendInfoById(id));
    }

    /**
     * 获取来样登记信息详细信息
     */
    @GetMapping("/getInfoByCompany")
    @ApiOperation(value="获取来样登记信息详细信息")
    public AjaxResult getInfoByCompany(@ApiParam(name="受检单位名称",value="传入字符串格式",required=true)@RequestParam String client, @ApiParam(name="来样日期",value="传入字符串格式",required=true)@RequestParam String reDate)
    {
        return AjaxResult.success(sampleSendInfoService.selectSampleSendInfoByCompany(client, reDate));
    }

    /**
     * 获取来样登记信息详细信息
     */
    @GetMapping("/getReDateByCompany")
    @ApiOperation(value="获取来样登记信息详细信息")
    @ApiImplicitParam(paramType="query", name = "client", value = "根据单位获取信息", required = true, dataType = "String")
    public AjaxResult getReDateByCompany(@RequestParam String client)
    {
        return AjaxResult.success(sampleSendInfoService.selectReDateByCompany(client));
    }


    /**
     * 新增来样登记信息
     */
    @Log(title = "来样登记信息", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value="新增来样登记信息")
    public AjaxResult add(@ApiParam(name="来样登记信息",value="传入json格式",required=true)@RequestBody SampleSendInfo sampleSendInfo)
    {
        return toAjax(sampleSendInfoService.insertSampleSendInfo(sampleSendInfo));
    }

    /**
     * 修改来样登记信息
     */
    @Log(title = "来样登记信息", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value="修改来样登记信息")
    public AjaxResult edit(@ApiParam(name="来样登记信息",value="传入json格式",required=true)@RequestBody SampleSendInfo sampleSendInfo)
    {
        return toAjax(sampleSendInfoService.updateSampleSendInfo(sampleSendInfo));
    }

    /**
     * 删除来样登记信息
     */
    @Log(title = "来样登记信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation(value="删除来样登记信息")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(sampleSendInfoService.deleteSampleSendInfoByIds(ids));
    }
}
